package com.elin4it.ssm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.elin4it.ssm.pojo.Answer;
import com.elin4it.ssm.pojo.GameRoom;
import com.elin4it.ssm.pojo.UserAnswer;
import com.elin4it.ssm.pojo.Userinfo;
import com.elin4it.ssm.service.AnswerService;
import com.elin4it.ssm.service.GameRoomService;
import com.elin4it.ssm.service.UserAnswerService;
import com.elin4it.ssm.service.UserinfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.elin4it.ssm.util.HttpsUtil;
import com.elin4it.ssm.util.UserInfoUtil;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author:TY
 * @Descroption:
 * @Date: Created in 16:21 2018/3/29
 * @Modificd By:
 */
@Controller
@RequestMapping(value = "/superadmin", method = RequestMethod.POST)//随机查询五道题目返回前台
public class answerController {
    @Resource(name = "UserinfoService")
    UserinfoService userinfoService;
    @Resource(name = "GameRoomService")
    GameRoomService gameRoomService;
    @Resource(name = "AnswerService")
    AnswerService answerService;
    @Resource(name = "UserAnswerService")
    UserAnswerService userAnswerService;
    public Logger logger = LoggerFactory.getLogger(getClass());
    public static final String WX_APPID = "wx111f9a9ca9dd1b6d";
    public static final String WX_APPSECRET = "64cc3177beb1b6f21b1c5c36e5979628";

    @RequestMapping(value = "/cqupt", method = RequestMethod.GET)//授权第一次获取openid
    @ResponseBody
    public Map<String, String> wecahtLogin(@RequestBody @RequestParam("code") String code) {
        // 1. 用户同意授权,获取code
        logger.info("收到微信重定向跳转.");
        logger.info("用户同意授权,获取code:{}", code);
        // 2. 通过code换取网页授权access_token
        Map<String, String> modlemap = new HashMap<String, String>();
        if (code != null || !(code.equals(""))) {
            String APPID = WX_APPID;
            String SECRET = WX_APPSECRET;
            String CODE = code;
            String WebAccessToken = "";
            String openId = "";
            String openid = "";
            // 替换字符串，获得请求access token URL
            String tokenUrl = UserInfoUtil.getWebAccess(APPID, SECRET, CODE);
            logger.info("第二步:get Access Token URL:{}", tokenUrl);
            // 通过https方式请求获得web_access_token
            String response = HttpsUtil.httpsRequestToString(tokenUrl, "GET", null);
            JSONObject jsonObject = JSON.parseObject(response);
            logger.info("请求到的Access Token:{}", jsonObject.toJSONString());
            if (null != jsonObject) {
                WebAccessToken = jsonObject.getString("access_token");
                openid = jsonObject.getString("openid");
                System.out.println(openId);
            }
            openId = openid;
            modlemap.put(new String("openid"), openId);
            System.out.println(modlemap);
        }
        return modlemap;
    }

    @RequestMapping(value = "/authorize", method = RequestMethod.POST)
    @ResponseBody
    public void Authorize(@RequestBody Userinfo userinfo) {
        userinfoService.insert(userinfo);
    }//插入用户的信息

    @RequestMapping(value = "/wait", method = RequestMethod.POST)
    @ResponseBody
    public void Wait(@RequestBody String opeinid) {
        userinfoService.updateStatusToZero(opeinid);
    }//把用户状态变为等待状态

    //    @RequestMapping(value = "/matching",method = RequestMethod.POST)
//    public Map Matching(@RequestBody  String openid)//前台定时发送请求
//    {
//     List<GameRoom> gameRoomList=gameRoomService.selectOpenidBelongToGameRoom(openid,openid);//查询这个人是否在房间里面
//        List<Answer> answerList=new ArrayList<Answer>();//用来储存题目号，题目内容，和答案
//     if(gameRoomList.size()==1)//如果有一条记录，说明匹配成功，
//     {
//                         //得到了题目号，题目内容和答案
//     }
//    }
    @RequestMapping(value = "/PassAnswer", method = RequestMethod.POST)
    @ResponseBody
    public void PassAnswer(@RequestBody UserAnswer userAnswer) {
        if (userAnswer.getResult1().equals("")) {  //获取result，及每个用户点击的答案
            userAnswer.setResult1(userAnswer.getResult1());
        } else {
            if (userAnswer.getResult2().equals("")) {
                userAnswer.setResult2(userAnswer.getResult2());
            } else {
                if (userAnswer.getResult3().equals("")) {
                    userAnswer.setResult3(userAnswer.getResult3());
                } else {
                    if (userAnswer.getResult4().equals("")) {
                        userAnswer.setResult4(userAnswer.getResult4());
                    } else {
                        if (userAnswer.getResult5().equals("")) {
                            userAnswer.setResult5(userAnswer.getResult5());
                        }
                    }
                }
            }
        }
        userAnswerService.updateByPrimaryKeySelective(userAnswer);//更新了分数和选项
    }
    @RequestMapping(value = "/Timer",method = RequestMethod.POST)//前台定时发送请求
            @ResponseBody
            public  void Timer(@RequestBody )
}

